<body>
    <div class="layuimini-container layui-anim layui-anim-upbit">
        <div class="layuimini-main">

            <fieldset class="table-search-fieldset">
                <legend>搜索信息</legend>
                <div style="margin: 10px 10px 10px 10px">
                    <form class="layui-form layui-form-pane" action="">
                        <div class="layui-form-item" style="text-align: center;">
                            <div class="layui-inline">
                                <label class="layui-form-label" style="width:90px;">专题集</label>
                                <div class="layui-input-inline" style="width:100px;height:38px;">
                                    <input type="text" name="chooseZTJ" id="chooseZTJ" autocomplete="off"
                                        class="layui-input" readonly placeholder="—全部—">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label" style="width:90px;">专题</label>
                                <div class="layui-input-inline" style="width:100px;height:38px;">
                                    <input type="text" name="chooseZT" id="chooseZT" autocomplete="off"
                                        class="layui-input" readonly placeholder="—全部—">
                                </div>
                            </div>

                            <div class="layui-inline">
                                <label class="layui-form-label" style="width:90px;margin-left: 10px;">检索方式</label>
                                <div class="layui-input-inline" style="width:100px;height:38px;">
                                    <select name="searchType" lay-filter="searchSelect">
                                    </select>
                                </div>
                            </div>

                            <div class="layui-inline">
                                <label class="layui-form-label" style="width:90px;">检索词</label>
                                <div class="layui-input-inline" style="width:100px;height:35px;">
                                    <input type="text" id="keyWord" placeholder="输入检索词" autocomplete="off"
                                        class="layui-input">
                                </div>
                            </div>

                            <div class="layui-inline">
                                <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="searchSubmit"
                                    style="margin-left:10px;width:90px;height:36px;border:none;outline:none;border-radius:6px;background-color:#007bff;color:white"><i
                                        class="layui-icon"></i> 搜 索</button>
                            </div>

                            <div class="layui-form-item">
                                <div id="standardType">
                                </div>
                            </div>
                        </div>
                        <div hidden>
                            <input type="text" name="chooseZTJId" id="chooseZTJId">
                            <input type="text" name="chooseZTId" id="chooseZTId">
                           
                        </div>
                    </form>
                </div>
            </fieldset>

            <div class="layui-form-item" style="padding-top:15px">
                <div class="layui-btn-container">

                    <button type="button" class="layui-btn layui-btn-warm layui-btn-radius" id="addStandard"
                        style="border-radius: 2px;"><i class="layui-icon layui-icon-form"></i> 添 加</button>
                </div>
            </div>

            <table class="layui-hide" id="artManage" lay-filter="currentTableFilter"></table>
            <div id="page"></div>

            <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit" >修改</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete" >删除</a>
            </script>
        </div>
    </div>

    <script>
        //定义表格数据
        var tableBar = [
            { align: 'center', title: '序号', type: 'numbers', width: '7%' },
            { field: 'ztjName', title: '专题集', width: '8%', align: "center" },
            { field: 'ztName', title: '专题', width: '8%', align: "center" },
            { field: 'ztSortId', title: '专题文献序号', width: '7%', align: "center" },
            { field: 'field', title: '领域', width: '7%', align: "center" },
            { field: 'title', title: '标题', width: '10%', align: "center" },
            { field: 'author', title: '作者', width: '10%', align: "center" },
            { field: 'keyWord', title: '关键词', width: '10%', align: "center" },
            { field: 'literatureSources', title: '文献来源', width: '10%', align: "center" },
            { field: 'publishDate', title: '发表时间', width: '10%', align: "center" },
            { title: '操作', width: "13%", templet: '#currentTableBar', align: "center" }
        ]

        //从配置中读取domain
        var allStandardTypeList = [],
            domainJson = config.json.domain;
        for (var index = 0; index < domainJson.length; index++) {
            var element = domainJson[index].dbCode;
            allStandardTypeList.push(element)
        }

        //选择专题集
        $("#chooseZTJ").on("click", function () {
            searchType = "search";
            var content = layui.miniPage.getHrefContent('mypages/knowledgeManagement/ztjList.html'),
                openWH = layui.miniPage.getOpenWidthHeight();
            var index = layer.open({
                title: '选择专题集',
                type: 1,
                shade: 0.2,
                maxmin: true,
                shadeClose: true,
                offset: '100px',
                area: ['450px', '500px'],
                btn: ['确认'],
                content: content
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;
        });

        //选择专题
        $("#chooseZT").on("click", function () {
            searchType = "search";
            if ($("input[name='chooseZTJ']").val() == "") {
                layui.layer.msg('请先选择专题集！', { icon: 7, time: 1000 })
            } else {
                chooseZtjId = $("input[name='chooseZTJId']").val();
                var content = layui.miniPage.getHrefContent('mypages/knowledgeManagement/ztList.html'),
                    openWH = layui.miniPage.getOpenWidthHeight();
                var index = layer.open({
                    title: '选择专题',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    offset: '100px',
                    btn: ['确认'],
                    area: ['450px', '500px'],
                    content: content
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            }
        });

        //传递参数
        var initTableParams = {
            "ztjId": "",
            "ztId": "",
            "searchWay": "",
            "keyWord": "",
            "standardType": allStandardTypeList,
            "page": 1,
            "pageSize": 10
        }

        //封装 表格渲染方法
        function tableRender(tableBar, initTableParams) {
            layuiEx.tableRender("/adminTechnicalBag/queryZTWX", 'artManage', 'page', tableBar,
                initTableParams
            );
        }

        //不能一个库都不选
        layui.form.on('checkbox(searchComxBox)', function (data) {
            var checkedCount = $('input[name=standardType]:checked').length
            if (checkedCount == 0) {
                layui.layer.msg('至少需要选择一个领域', { icon: 7, time: 1000 })
                data.elem.checked = true
                layui.form.render()
            }
        })

        //收集页面参数
        function collectParams() {
            var res = {},
                standardTypeList = [],
                searchWay = $("select[name='searchType']").val(),
                keyWord = $("#keyWord").val(),
                ztjId = $("#chooseZTJId").val(),
                ztId = $("#chooseZTId").val();
            //获取勾选领域
            $("input[name='standardType']:checked").each(function (params) {
                standardTypeList.push($(this).attr('lay-value'))
            })
            res.standardType = standardTypeList
            res.searchWay = searchWay
            res.keyWord = keyWord
            res.ztjId = ztjId
            res.ztId = ztId
            res.page = "1"
            res.pageSize = "10"
            return res
        }

        //恢复初始化
        function getInit() {
            $("#keyWord").val("");
            $("#chooseZTJId").val("");
            $("#chooseZTId").val("");
            $("#chooseZTJ").val("");
            $("#chooseZT").val("");
            var length = document.getElementsByName('standardType').length;
            for(var i = 0; i<length; i++){
                document.getElementsByName('standardType')[i].checked = true;
            }
        }

        //input框悬停显示
        var tip_index = 0;
        $('.layui-input').hover(function () {
            if (this.value) {
                tip_index = layui.layer.tips(this.value, this, { tips: [1, '#9c9da0'], time: 30000 });
            }
        }, function () {
            layui.layer.close(tip_index);
        });

        //初始化配置数据
        $(function () {
            //构造检索下拉框
            var searchSelect = [{ "题名": "题名" }, { "作者": "作者" }, { "关键词": "关键词" }, { "全文": "全文" }]
            for (var index = 0; index < searchSelect.length; index++) {
                var pros = []
                for (var i in searchSelect[index]) {
                    pros.push(i)
                }
                var theValue = pros[0]
                var opt = document.createElement('option')
                opt.setAttribute('value', theValue)
                opt.innerText = theValue
                document.getElementsByName('searchType')[0].appendChild(opt)
            }

            //构造标准复选框
            var domainJson = config.json.domain
            for (var index = 0; index < domainJson.length; index++) {
                var element = domainJson[index];
                document.getElementById('standardType').innerHTML += '<input type="checkbox" lay-filter="searchComxBox" lay-value="' + element.dbCode + '" name="standardType" lay-skin="primary" title="' + element.showName + '" checked="">'
            }
        })

        //页面初始化渲染table
        tableRender(tableBar, initTableParams)

        //监听搜索
        layui.form.on('submit(searchSubmit)', function (params) {
            tableRender(tableBar, collectParams())
            return false
        })

        //渲染页面
        layui.form.render();

        // 监听编辑操作
        layui.table.on('tool(currentTableFilter)', function (obj) {
            if (obj.event === 'edit') {
                getInit();
                //定义数据,子组件调用
                editData = obj.data;
                opreatType = "edit";
                var content = layui.miniPage.getHrefContent('mypages/knowledgeManagement/addZTWX.html');
                var openWH = layui.miniPage.getOpenWidthHeight();
                var index = layer.open({
                    title: '编辑专题文献',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['1200px', '90%'],
                    content: content,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('确定删除 ' + obj.data.title + " ?", function (index) {
                    shared.ajaxPost('/adminTechnicalBag/deleteZTWX', obj.data, function (res) {
                        tableRender(tableBar, collectParams())
                    })
                    layer.close(index);
                });
            }
        });

        // 监听添加操作
        $("#addStandard").on("click", function () {
            getInit();
            var content = layui.miniPage.getHrefContent('mypages/knowledgeManagement/addZTWX.html');
            var openWH = layui.miniPage.getOpenWidthHeight();
            
            opreatType = "add";
            var index = layer.open({
                title: '添加专题文献',
                type: 1,
                shade: 0.2,
                area: ['1200px', '90%'],
                maxmin: true,
                shadeClose: false,
                content: content,
                success: function () {
                    $(':focus').blur()
                }
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;
        });
    </script>
</body>